package cn.tqfeiyang.springplus.framework.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 用户详情服务，根据用户名查找用户详情。
 *
 * @author tqfeiyang
 * @since 2024-07-19
 */
@Component
public class AuthenticationUserDetailsService implements UserDetailsService {

    @Autowired
    private AuthenticationUserLoader userLoader;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthenticationUserDetails userDetails = userLoader.loadByUsername(username);
        if (Objects.isNull(userDetails)) {
            throw new UsernameNotFoundException("Username not found: " + username);
        }
        return userDetails;
    }

}
